package com.example.demo.service.impl;

import com.example.demo.dao.UserMapper;
import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import com.example.demo.utils.MD5Util;
import com.example.demo.utils.Result;
import com.example.demo.utils.ResultFactory;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * User Service
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    private Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    /**
     * 修改密码
     * @param reader_id
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @Override
    public Result updatePasword(int reader_id, String oldPassword, String newPassword) {

        String password = this.userMapper.selUserByReaderid(reader_id);
        if(!MD5Util.stringToMD5(oldPassword).equals(password)){
            //旧密码不正确
            return ResultFactory.failure("旧密码错误");
        }
        //旧密码正确，修改密码
        this.userMapper.updateUserPasswordByReaderid(reader_id, MD5Util.stringToMD5(newPassword));

        return ResultFactory.successWithStatus(200,"密码修改成功");
    }

    @Override
    public Result checkStunum(String stunum) {
        List<User> users = this.userMapper.selUserByStuNum(stunum);
        if(users!=null||users.size()>0){
            return ResultFactory.failureWithStatus(200,stunum+"账号已存在");
        }
        return ResultFactory.successWithStatus(999,"账号不存在");
    }

    @Override
    public Result saveUser(User user) {

        user.setPassword(MD5Util.stringToMD5(user.getPassword()));
        int i = this.userMapper.insertUser(user);
        //logger.info("UserServiceImpl i:" + i);
        if(user.getUser_state() == 2){
            return ResultFactory.successWithStatus(200, "注册借阅卡成功");
        }else {
            return ResultFactory.successWithStatus(200, "添加管理员成功");
        }
    }

    @Override
    public Result updateUserStateByReaderId(int reader_id,int state) {

        this.userMapper.updateUserStateById(reader_id,state);
        if(state == 2) {
            return ResultFactory.successWithStatus(200, "借阅证暂停成功");
        }else if(state == 4){
            return ResultFactory.successWithStatus(200, "借阅证注销成功");
        }else if(state == 3){
            return ResultFactory.successWithStatus(200, "借阅证挂失成功");
        }else{
            return ResultFactory.failureWithStatus(999,"没有此操作");
        }
    }

    @Override
    public Result selReaderInfoById(int reader_id) {
        User user = this.userMapper.findUserByReaderId(reader_id);
        if(user == null){
            return ResultFactory.failureWithStatus(999,"没有此用户");
        }
        return ResultFactory.successWithStatusAndData(200,"查找成功",user);
    }

    @Override
    public Result updateReaderInfo(User user) {
        this.userMapper.updateUserByReaderId(user);
        return ResultFactory.successWithStatus(200,"借阅卡信息修改成功");
    }

    @Override
    public PageBean<User> selAllUser(int currentPage,int pageSize,int user_state) {
        int colNums = this.userMapper.findAllUserCount(user_state);
        PageHelper.startPage(currentPage,pageSize);
        List<User> allUser = this.userMapper.findAllUser(user_state);
        PageInfo<User> userPageInfo = new PageInfo<>(allUser);
        List<User> list = userPageInfo.getList();
        PageBean<User> pageData = new PageBean<>(currentPage,pageSize,colNums);
        pageData.setItem(list);
        return pageData;
    }

    @Override
    public PageBean<User> selAllUserByConditions(int currentPage, int pageSize,User user, int user_state) {
        user.setUser_state(user_state);
        int colNums = this.userMapper.findAllUserCountByConditions(user);
        PageHelper.startPage(currentPage,pageSize);
        List<User> users = this.userMapper.findAllUserByConditions(user);
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        List<User> list = userPageInfo.getList();
        PageBean<User> pageData = new PageBean<>(currentPage,pageSize,colNums);
        pageData.setItem(list);
        return pageData;
    }
}
